﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Util;
using System.IO;
using System.Xml;
using log4net;
using System.Reflection;

namespace WebCollection
{
    public class Storage
    {
        //private ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public string configroot { get; set; }

        public string saveroot { get; set; }

        public Storage()
        {
            configroot = ConfigUtil.GetAppSetting("configroot");
            saveroot = ConfigUtil.GetAppSetting("saveroot");
        }

        public List<string> GetConfigs()
        {
            string[] arr = Directory.GetFiles(configroot, "*.xml", SearchOption.AllDirectories);
            return arr.ToList();
        }

        public string GetSavePath(string _filename)
        {
            //D:\TIANDITU\CONFIG\大众点评网\川菜\北京\模板.xml
            string filename = _filename.Replace(@"D:\TIANDITU\CONFIG\", "").Replace("\\模板.xml", "");
            string temp = Path.Combine(saveroot, filename);
            return temp;
            ////E:\牧童在线\牧童配置与数据\config\苏宁\图书\综合图书\音像制品\模板.xml
            //string filename = _filename.Replace(configroot, "");
            //string[] arr = filename.Split('\\');
            //List<string> ls = arr.Reverse().ToList();
            //string[] arr_1 = ls.Skip(1).Take(2).ToArray();
            //string relative = string.Format(@"{1}\{0}", arr_1[0], arr_1[1]);
            //string path = Path.Combine(saveroot, relative);
            //return path;
        }

        /// <summary>
        /// 获取已经抓取过配置的文件名(还是直接检测文件夹下是否有文件吧)
        /// </summary>
        public List<string> FileCrawled(string path)
        {
            string relative = path.Replace(saveroot, "");
            string sitename = relative.Split('\\').ToList()[0];
            string dir = Path.Combine(saveroot, sitename);
            string file = Directory.GetFiles(dir, "*.txt")[0];

            string[] xmls = File.ReadAllLines(file);
            return xmls.ToList();
        }


        /// <summary>
        /// 清除掉已经抓取过的(只要目录下有files则认为抓取了)
        /// </summary>
        /// <param name="files"></param>
        public List<string> ClearCrawled(List<string> files)
        {
            if (files == null || files.Count < 1)
                return null;
            List<string> list = new List<string>();//未抓取的
            foreach (string file in files)
            {
                string dir = file.Replace("config", "data").Replace("模板.xml", "");

                //文件夹不存在，则是未抓取的
                if (!Directory.Exists(dir)) 
                {
                    list.Add(file);
                    continue;
                }

                //文件夹存在，但里面无数据，有可能这个栏目本身就没数据，有可能没抓，算做未抓吧
                string[] arr = Directory.GetFiles(dir, "*.xml");
                if (arr == null || arr.Count() == 0)//未抓取
                {
                    list.Add(file);
                }
            }
            Console.WriteLine("清理已经抓取的配置文件个数 : " + (files.Count - list.Count));
            return list;
        }
    }
}
